package com.zensun.web.controller.system;

import com.zensun.common.annotation.Log;
import com.zensun.common.core.controller.BaseController;
import com.zensun.common.core.domain.BaseEntity;
import com.zensun.common.core.domain.R;
import com.zensun.common.core.page.PageDataInfo;
import com.zensun.common.enums.BusinessType;
import com.zensun.system.domain.SysDictData;
import com.zensun.system.domain.dto.SysDictDataDto;
import com.zensun.system.domain.vo.SysDictDataSelectVo;
import com.zensun.system.domain.vo.SysDictDataVo;
import com.zensun.system.service.ISysDictDataService;
import com.zensun.system.service.ISysDictTypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 数据字典信息
 *
 * @author gmk
 */
@Api(value = "数据字典数据管理", tags = "数据字典数据模块")
@RestController
@RequestMapping("/system/dict/data")
public class SysDictDataController extends BaseController {
    @Autowired
    private ISysDictDataService dictDataService;

    @Autowired
    private ISysDictTypeService dictTypeService;

    @ApiOperation("查询数据字典数据列表")
    @PreAuthorize("@ss.hasPermi('system:dictdata:list')")
    @GetMapping("/list")
    public R<PageDataInfo<SysDictDataVo>> list(SysDictDataDto dictDataDto) {
        startPage();
        List<SysDictDataVo> sysDictDataVos = dictDataService.selectDictDataList(dictDataDto);
        return R.success(getPageDataTable(sysDictDataVos));
    }

    /**
     * 查询字典数据详细
     */
    @ApiOperation("查询字典数据详细")
    @PreAuthorize("@ss.hasPermi('system:dictdata:query')")
    @GetMapping(value = "/{dictCode}")
    public R<SysDictDataVo> getInfo(@ApiParam("字典数据主键") @PathVariable Long dictCode) {
        return R.success(dictDataService.selectDictDataId(dictCode));
    }

    /**
     * 根据字典类型查询字典数据信息下拉框
     */
    @ApiOperation("根据字典类型查询字典数据信息下拉框")
    @GetMapping(value = "/select/{dictType}")
    public R<List<SysDictDataSelectVo>> getDictDataByDictType(@ApiParam("字典数据类型") @PathVariable String dictType) {
        return R.success(dictTypeService.getDictDataByDictType(dictType));
    }

    /**
     * 新增字典数据
     */
    @ApiOperation("新增字典数据")
    @PreAuthorize("@ss.hasPermi('system:dictdata:add')")
    @Log(title = "字典数据", businessType = BusinessType.INSERT)
    @PostMapping
    public R add(@Validated(BaseEntity.AddGrop.class) @RequestBody SysDictData dict) {
        return R.toAjax(dictDataService.insertDictData(dict));
    }

    /**
     * 修改字典数据
     */
    @ApiOperation("修改字典数据")
    @PreAuthorize("@ss.hasPermi('system:dictdata:edit')")
    @Log(title = "字典数据", businessType = BusinessType.UPDATE)
    @PutMapping
    public R edit(@Validated(BaseEntity.UpdateGrop.class) @RequestBody SysDictData dict) {
        return R.toAjax(dictDataService.updateDictData(dict));
    }

    /**
     * 删除字典数据
     */
    @ApiOperation("删除字典数据")
    @PreAuthorize("@ss.hasPermi('system:dictdata:remove')")
    @Log(title = "字典类型", businessType = BusinessType.DELETE)
    @DeleteMapping("/{dictCodes}")
    public R remove(@ApiParam("主键集合（1,2）") @PathVariable Long[] dictCodes) {
        return R.toAjax(dictDataService.deleteDictDataByIds(dictCodes));
    }
}
